home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Utilities / Graphic / Conversion / viewers / GL Viewer 1.1.1 / src ƒ / xlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-05  |  16.8 KB  |  944 lines  |  [TEXT/KAHL]

  1. /*
  2.   xlib.c
  3.     mac hack at x window lib
  4.     oct91
  5. */
  6.  
  7. #pragma segment XLib
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include <console.h>
  14. #include <Palettes.h>
  15.  
  16. #include "glassert.h"
  17. #include "xlib.h"
  18.  
  19.  
  20. extern void    exitcheck (void);
  21. static void    SetPortPenColors (Display *dsp,
  22.                               RGBColor *pRGBForeColor, RGBColor *pRGBBackColor);
  23. static void    CopyBitsPrep (Display *dsp, char fSave);
  24.  
  25.  
  26. #define    True 1
  27. #define False 0
  28.  
  29. typedef unsigned short u_short;
  30. typedef unsigned long  u_long;
  31.  
  32.  
  33. static WindowPtr        cw = (WindowPtr) NULL;    
  34.  
  35.  
  36. XImage *XCreateImage(
  37.     Display *dsp,
  38.     Visual *vis,
  39.     unsigned int depth,
  40.     int format,
  41.     int offset,
  42.     char *data,
  43.     unsigned int width,
  44.     unsigned int height,
  45.     int bitmap_pad,
  46.     int bytes_per_line)
  47. {
  48.     XImage             *xim;
  49.     long              rowbytes;
  50.     unsigned int     hi1 = height;
  51.  
  52.  
  53.     // -- this is how we made data ptr
  54.     //datasize = bpsl * im->h;
  55.     //data = (u_char *) malloc(datasize);
  56.     //bytes_per_line == bpsl
  57.     //heigth == im->h
  58.     
  59.     // fix for Mac bitmap -- bytes_per_line must be word-aligned (2-byte)
  60.     rowbytes = bytes_per_line;
  61.     if (bytes_per_line & 1) {
  62.         char     *tbuf;
  63.         unsigned long i;
  64.     
  65.         rowbytes= bytes_per_line+1;
  66.         tbuf = (char*) NewPtrClear( (size_t) rowbytes*hi1 + 4);
  67.         assert (tbuf);
  68.  
  69.         if ((u_long)tbuf & 3 !=0)
  70.           tbuf = (char *) (((u_long)tbuf & 0xfffffffc) + 4); // make it long-aligned
  71.  
  72.         for (i=0; i<hi1; i++)
  73.           memcpy( tbuf +(i*rowbytes), data +(i*bytes_per_line), bytes_per_line);
  74.         free(data);
  75.         data = tbuf;
  76.         }
  77.  
  78.     xim = (XImage *) malloc((size_t) sizeof(XImage));
  79.     assert (xim);
  80.     //use vis to set rgb, ZPixmap stuff    
  81.     xim->width = width;
  82.     xim->height = height;
  83.     xim->format = format;
  84.     xim->data = data;
  85.     xim->depth = depth;
  86.     xim->xoffset = offset;
  87.     xim->bitmap_pad = bitmap_pad;
  88.     xim->bytes_per_line = rowbytes; //was bytes_per_line; << fix for mac
  89.     return xim;
  90. }
  91.  
  92. void XPutImage(
  93.     Display *dsp,
  94.     Drawable win,
  95.     GC gc,
  96.     XImage *image,
  97.     int src_x,
  98.     int src_y,
  99.     int dest_x,
  100.     int dest_y,
  101.     unsigned int width,
  102.     unsigned int height)
  103. {
  104. // display image
  105.  
  106. // !! this is BAD, win is not &variable, value is not returned !!!
  107. //    win = image->data;
  108. }
  109.  
  110.  
  111. #define UseColorWin     1
  112.  
  113. static PixMapHandle     srcpixmap, winpixmap;
  114. static CTabHandle        theColorTab = (CTabHandle) NULL;
  115. static CTabHandle        theDefaultColorTab = (CTabHandle) NULL;
  116.  
  117.  
  118. void XCopyArea(
  119.     Display *dsp,
  120.     Drawable src,
  121.     Drawable dest,
  122.     GC gc,
  123.     int src_x,
  124.     int src_y,
  125.     unsigned int width,
  126.     unsigned int height,
  127.     int dest_x,
  128.     int dest_y)
  129. {
  130. #define cw        ((WindowPtr)dsp)
  131. #define xim        ((XImage *)src)
  132.     extern XRectangle window;
  133.     Rect          sr, dr;
  134.     int            i;
  135.     int            dx;
  136.     int            dy;
  137.  
  138.  
  139.     if (src==NULL) return;
  140.  
  141.     if (src_x < 0)
  142.     {
  143.         dest_x -= src_x;
  144.         width  += src_x;
  145.         src_x   = 0;
  146.     }
  147.     if (src_y < 0)
  148.     {
  149.         dest_y -= src_y;
  150.         height += src_y;
  151.         src_y   = 0;
  152.     }
  153.  
  154.     if (src_x + width > xim->width)
  155.         width = xim->width - src_x;
  156.     if (src_y + height > xim->height)
  157.         height = xim->height - src_y;
  158.  
  159.     if (height > xim->height)
  160.         height = xim->height;
  161.     if (width > xim->width)
  162.         width = xim->width;
  163.  
  164.     if (width < 0 || height < 0)
  165.         return;
  166.  
  167.     if (src_x > xim->width || src_y > xim->height)
  168.         return;
  169.  
  170.     dx = dest_x;
  171.     dy = dest_y;
  172.     dx += (cw->portRect).left;
  173.     dy += (cw->portRect).top;
  174.  
  175.     SetRect( &dr, dx, dy, dx + width, dy + height);
  176.     SetRect( &sr, src_x, src_y, src_x + width, src_y + height);
  177.     
  178. #ifdef UseColorWin
  179. #define db    ((BitMap*) (*winpixmap))
  180. #define sp  ((PixMapPtr) (*srcpixmap))
  181. #define sb    ((BitMap*) (*srcpixmap))
  182. #define pixmapFlag    0x8000
  183.  
  184.     sp->pixelSize = xim->depth;
  185.     sp->cmpSize = sp->pixelSize;
  186.     sp->baseAddr = (Ptr) xim->data;
  187.     sp->rowBytes = xim->bytes_per_line;
  188.     if (sp->pixelSize > 1 && theColorTab /* != (CTabHandle) NULL */)
  189.     {
  190.         CopyBitsPrep (dsp, True);
  191.         sp->pmTable      = theColorTab;
  192.         sp->rowBytes |= pixmapFlag;
  193.     }
  194.     SetRect( &sp->bounds, 0, 0, xim->width, xim->height);
  195.     
  196.     CopyBits( sb, db, &sr, &dr, srcCopy, NULL);
  197.  
  198.     if (sp->pixelSize > 1)
  199.         CopyBitsPrep (dsp, False);
  200.     
  201. #else
  202. #define db    (&(cw->portBits))
  203. {
  204.     BitMap* sb = (BitMap*) NewPtr(sizeof(BitMap));
  205.     
  206.     sb->baseAddr = (Ptr) xim->data;
  207.     sb->rowBytes = xim->bytes_per_line;
  208.     SetRect( &sb->bounds, 0, 0, xim->width, xim->height);
  209.     CopyBits( sb, db, &sr, &dr, srcCopy, NULL);
  210.     DisposPtr((Ptr) sb);
  211. }
  212. #endif
  213.  
  214. #undef cw
  215. #undef xim
  216. #undef db
  217. #undef sb
  218. #undef sp
  219. }
  220.  
  221.  
  222. Pixmap XCreatePixmap(
  223.     Display *dsp,
  224.     Drawable win,
  225.     unsigned int width,
  226.     unsigned int height,
  227.     unsigned int depth)
  228. {
  229.     //Pixmap *pix = NewPtr(sizeof(Pixmap));
  230.     //return *pix;
  231.     return NULL;
  232. }
  233.  
  234.  
  235. void  XFreePixmap(
  236.     Display *dsp,
  237.     Pixmap pix)
  238. {
  239.     // DisposPtr((ptr)pix);
  240. }
  241.  
  242.  
  243. Display *XOpenDisplay( char* name)
  244. {
  245.    Rect  bounds;
  246.    const Bool  Visible = True;
  247.    Str255    fName;
  248.  
  249.  
  250.     assert (name);
  251.  
  252.     strcpy ((char *) fName, name);
  253.     SetRect(&bounds, 2, 40, 322, 240);
  254. #ifdef UseColorWin
  255.     cw= NewCWindow( NULL, &bounds, CtoPstr (fName), Visible, 0, NULL, False, 0);
  256.     srcpixmap = NewPixMap();
  257.     winpixmap = ((CWindowPtr) cw)->portPixMap;
  258.     CopyPixMap( winpixmap, srcpixmap);
  259.     theColorTab = (**winpixmap).pmTable; // set default
  260.     (**theColorTab).ctSeed = GetCTSeed ();
  261.  
  262.     if (theDefaultColorTab == (CTabHandle) NULL &&
  263.         (theDefaultColorTab = theColorTab) /* != (CTabHandle) NULL */)
  264.         HandToHand (&theDefaultColorTab);
  265. #else
  266.     cw= newwindow( NULL, &bounds, name, Visible, 0, NULL, False, 0);
  267. #endif
  268.  
  269.     SelectWindow( cw);
  270.     SetPort(cw);
  271.     MoveTo( 9, 40); DrawString("\pWorking...");
  272.     MoveTo( 9, 90); DrawString("\p(command-period to halt)");
  273.  
  274.     return ((Display*) cw);
  275. }
  276.  
  277.  
  278.     void
  279. XCloseDisplay (Display *dpy)
  280.  
  281. {
  282.     if (dpy /* != (Display *) NULL */ &&
  283.         theDefaultColorTab /* != (CTabHandle) NULL */)
  284.     {
  285.         WindowPtr        pWindow       = (WindowPtr) dpy;
  286.         PixMapHandle    hWinPixMap = ((CWindowPtr) pWindow)->portPixMap;
  287.  
  288.  
  289.         (*theDefaultColorTab)->ctSeed = GetCTSeed ();
  290.         (*hWinPixMap)->pmTable = theDefaultColorTab; // set default
  291.  
  292.         CopyBitsPrep (dpy, TRUE);
  293.  
  294.         SelectWindow (pWindow);
  295.         ActivatePalette (pWindow);
  296.  
  297.         (void) usleep (0L);
  298.     }
  299. }
  300.  
  301.  
  302. XResizeWindow( dsp, win,  width, height)
  303.     Display *dsp;
  304.     Window win;
  305.     unsigned int width, height;
  306. {
  307.    SizeWindow( (WindowPtr) dsp, width, height, True);
  308. }
  309.  
  310.  
  311.  
  312. #define MAXPALCOLS     256
  313.  
  314. Colormap XCreateColormap(     
  315.     Display *dsp,
  316.     Window win,
  317.     Visual *vis,
  318.     int allocflag)
  319. {
  320.    CTabHandle    cmap;     // CSpecArray == ColorSpec[]
  321. #define  ctab    (**cmap).ctTable
  322.    int            i;
  323.    int            acolor;
  324.    const int    black = 0;
  325.    const int    white = 0xffff;
  326.  
  327.    cmap = (CTabHandle) NewHandle(sizeof(ColorTable) + MAXPALCOLS*sizeof(ColorSpec));
  328.    (**cmap).ctSeed = GetCTSeed();
  329.    (**cmap).ctFlags = 0x0000;
  330.    (**cmap).ctSize = MAXPALCOLS-1;
  331.    acolor = white;
  332.    for (i=0;i<MAXPALCOLS; i++) {
  333.      ctab[i].value = i;
  334.      ctab[i].rgb.red = ctab[i].rgb.green = ctab[i].rgb.blue = acolor;
  335.      acolor = black;
  336.      }
  337.    return (Colormap) cmap;
  338. #undef  ctab
  339. }
  340.  
  341. void XStoreColors(
  342.     Display *dsp,
  343.     Colormap  cmap,
  344.     XColor color[],
  345.     int  ncolors)
  346. {
  347. #define ctab    (**(CTabHandle)cmap).ctTable
  348.   int        i;
  349.   Bool        ciused[MAXPALCOLS];
  350.   Bool        done;
  351.  
  352.  
  353.   assert (cmap);
  354.   if (ncolors /* > 0 */)
  355.     assert (color);
  356.  
  357.   for (i=0; i<MAXPALCOLS; i++) ciused[i]=False;
  358.   (**(CTabHandle)cmap).ctSeed = GetCTSeed(); // notice that we have new colors???
  359.   if (ncolors>MAXPALCOLS) ncolors=MAXPALCOLS;
  360.   for (i=0; i<ncolors; i++) {
  361.     ctab[i].rgb.red     =  color[i].red;
  362.     ctab[i].rgb.green     =  color[i].green;
  363.     ctab[i].rgb.blue     =  color[i].blue;
  364.     }
  365.      
  366. /*******
  367.     // need to set indices to Device/Std CLUT so we aren't totally mangling
  368.     // out-of-window colors on SetColormap...
  369.     // Can we assume this is called when Std CLUT is in place ?? -- probably not
  370.     if (RealColor(&ctab[i].rgb)) {
  371.       short cind = (short) Color2Index(&ctab[i].rgb);
  372.       ctab[i].value = cind;
  373.       if (cind < MAXPALCOLS) ciused[cind] = True;
  374.       //color[i].pixel = cind;
  375.       }
  376.     else {
  377.       ctab[i].value = MAXPALCOLS; // ?? key we need to force clut ?
  378.       }
  379.  *******/
  380.  /*********
  381.   for (i=0; i<ncolors; i++) {
  382.     if (ctab[i].value == MAXPALCOLS) {
  383.         int        j;
  384.         Bool    done;
  385.         for (j=0, done=False; (!done) && (j<MAXPALCOLS); j++) {
  386.             ctab[i].value = i;
  387.             ciused[j] = True;
  388.             done=True;
  389.             }
  390.         if (!done)
  391.             ctab[i].value = (short) Color2Index(&ctab[i].rgb);
  392.         }
  393.     }
  394.  ************/
  395.  
  396. #undef  ctab
  397. }
  398.  
  399.  
  400.  
  401. void XSetWindowColormap(
  402.     Display *dsp,
  403.     Drawable win,
  404.     Colormap cmap)
  405. {
  406. #ifdef UseColorWin
  407.  
  408.     CTabHandle    devCmap;
  409.     int            i;
  410.     Bool          ciused[MAXPALCOLS];
  411.     Bool          cineed[MAXPALCOLS];
  412. #define ctab    (**(CTabHandle)cmap).ctTable
  413. #define dtab    (**(CTabHandle)devCmap).ctTable
  414.  
  415.  
  416.     assert (cmap);
  417.  
  418.     theColorTab = (CTabHandle)cmap;
  419.     (**winpixmap).pmTable = theColorTab;
  420.  
  421. // !need this when ColorTab includes colors not in standard Color Lookup Table
  422.     HLock((Handle)theColorTab);
  423.     SetEntries( 0, (**theColorTab).ctSize, (**theColorTab).ctTable);
  424.     HUnlock((Handle)theColorTab);
  425.  
  426. // use by-index installation (-1) in Device CLUT, so we preserve out-of-window colors
  427. // as much as possible ?????
  428. //     SetEntries( -1, (**theColorTab).ctSize, (**theColorTab).ctTable);
  429.     
  430. /***********
  431.     // identify colors in current CLUT (ciused) & those not in (cineed)
  432.     for (i=0; i<MAXPALCOLS; i++) ciused[i]= False;
  433.     for (i=0; i<<=(**theColorTab).ctSize; i++) cineed[i]= False;
  434.     for (i=0; i<=(**theColorTab).ctSize; i++)
  435.         if (RealColor(&ctab[i].rgb)) {
  436.           short cind = (short) Color2Index(&ctab[i].rgb);
  437.           ciused[cind]= True;
  438.           }
  439.         else {
  440.           cineed[i]= True;
  441.           }
  442.             
  443.     // copy current CLUT
  444.     devCmap = (CTabHandle) NewHandle(sizeof(ColorTable) + MAXPALCOLS*sizeof(ColorSpec));
  445.     (**devCmap).ctSize = MAXPALCOLS-1;
  446.     (**devCmap).ctSeed = GetCTSeed();
  447.     (**devCmap).ctFlags = 0x0000;
  448.     // ?? use this --
  449.     for (i=0; i<MAXPALCOLS; i++) { Index2Color( i, &dtab[i].rgb); dtab[i].value=i; }
  450.     
  451.     // replace entries in current CLUT w/ new colors that are needed
  452.     for (i=0; i<=(**theColorTab).ctSize; i++)
  453.         if (cineed[i]) {
  454.           int    j;
  455.           Bool    done;
  456.           for (j=0, done=False; (!done) && (j<MAXPALCOLS); j++)
  457.             if (!ciused[j]) {
  458.               dtab[j].rgb = ctab[i].rgb;
  459.               done= True;
  460.               }
  461.           }
  462.     
  463.     // store revised CLUT
  464.     HLock((Handle)devCmap);
  465.     SetEntries( 0, (**devCmap).ctSize, (**devCmap).ctTable);
  466.     HUnlock((Handle)devCmap);
  467.     DisposHandle((Handle)devCmap);
  468. *********/
  469.  
  470. #undef ctab
  471. #undef dtab
  472.  
  473. #endif
  474. }
  475.  
  476. int XAllocColorCells(
  477.     Display *dsp,
  478.     Colormap  cmap,
  479.     Bool  contig,
  480.     unsigned long plane_masks_return[],
  481.     unsigned int nplanes,
  482.     unsigned long pixels_return[],
  483.     unsigned int npixels)
  484. {
  485.   return 0;
  486. }
  487.  
  488.  
  489. void XSetForeground(
  490.     Display *dsp,
  491.     GC gc,
  492.     unsigned long foreground)
  493. {
  494.     WindowPtr    aPort;
  495.  
  496.     
  497.     GetPort (&aPort);
  498.     SetPort ((WindowPtr) dsp);
  499.  
  500. #if 0
  501.     if (theColorTab /* != (CTabHandle) NULL */)
  502.     {
  503.         if (--foreground <= (*theColorTab)->ctSize)
  504.         {
  505.             RGBColor   *pColor = &((*theColorTab)->ctTable[foreground].rgb);
  506.  
  507.  
  508.             SetPortPenColors (dsp, pColor, (RGBColor *) NULL);
  509.         }
  510.     }
  511. #else
  512.     PmForeColor (foreground - 1);
  513. #endif
  514.  
  515.     SetPort (aPort);
  516. }
  517.  
  518. void XSetBackground(
  519.     Display *dsp,
  520.     GC gc,
  521.     unsigned long background)
  522. {
  523.     WindowPtr    aPort;
  524.  
  525.     
  526.     GetPort (&aPort);
  527.     SetPort ((WindowPtr) dsp);
  528.  
  529. #if 0
  530.     if (theColorTab /* != (CTabHandle) NULL */)
  531.     {
  532.         if (--background <= (*theColorTab)->ctSize)
  533.         {
  534.             GrafPtr        oldPort;
  535.             RGBColor   *pColor = &((*theColorTab)->ctTable[background].rgb);
  536.  
  537.  
  538.             SetPortPenColors (dsp, (RGBColor *) NULL, pColor);
  539.         }
  540.     }
  541. #else
  542.     PmBackColor (background - 1);
  543. #endif
  544.  
  545.     SetPort (aPort);
  546. }
  547.  
  548.  
  549. static Pixmap stipplePixmap  = (Pixmap) NULL;
  550. static int      stippleStyle     = FillSolid;
  551. static int      stippleXOrigin = 0;
  552. static int      stippleYOrigin = 0;
  553.  
  554.  
  555.     void
  556. XSetFillStyle (Display *dsp, GC gc, int    fill_style)
  557.  
  558. {
  559.     stippleStyle = fill_style;
  560. }
  561.  
  562.  
  563.     void
  564. XSetStipple (Display *dsp, GC gc, Pixmap stipple)
  565.  
  566. {
  567.     if (stipple /* != (Pixmap) NULL */)
  568.         stipplePixmap = stipple;
  569.     else
  570.         stippleStyle = FillSolid;
  571. }
  572.  
  573.  
  574.     void
  575. XSetTSOrigin (Display *dsp, GC gc, int ts_x_origin, int ts_y_origin)
  576.  
  577. {
  578.     stippleXOrigin = ts_x_origin;
  579.     stippleYOrigin = ts_y_origin;
  580. }
  581.  
  582.  
  583. void XFillRectangle(
  584.     Display *dsp,
  585.     Drawable win,
  586.     GC gc,
  587.     int x,
  588.     int y,
  589.     int width,
  590.     int height)
  591. {
  592.     WindowPtr    aPort;
  593.     Rect        dr;
  594.     
  595.     GetPort (&aPort);
  596.     SetPort ((WindowPtr) dsp);
  597.  
  598.     SetRect( &dr, x,y,x+width,y+height);
  599.  
  600.     if (stippleStyle == FillStippled && stipplePixmap /* != (Pixmap) NULL */)
  601.     {
  602.     }
  603.     else
  604.     {
  605. #if 1
  606.         PaintRect (&dr);
  607. #else
  608.         FillRect (&dr, white);
  609. #endif
  610.     }
  611.  
  612.     SetPort (aPort);
  613. }
  614.  
  615.  
  616. void    XFillRectangles(
  617.     Display *dsp,
  618.     Drawable win,
  619.     GC gc,
  620.     XRectangle *rects,
  621.     int nrects)
  622. {
  623. #define setxrects(ar, r) \
  624.     SetRect( &ar, (r).x, (r).y, (r).x+(r).width, (r).y+(r).height)
  625.  
  626.     WindowPtr    aPort;
  627.     Rect        aRec;
  628.     int            i;
  629.  
  630.  
  631.     if (nrects /* > 0 */)
  632.         assert (rects);
  633.  
  634.     GetPort( &aPort);
  635.     SetPort((WindowPtr)dsp);
  636.     for (i=0; i<nrects; i++) {
  637.       setxrects(aRec, rects[i]);
  638.       PaintRect(&aRec);
  639.       }
  640.     SetPort(aPort);
  641. }
  642.  
  643.  
  644.     void
  645. XDrawLine (Display *dsp, Drawable win, GC gc, int x1, int y1, int x2, int y2)
  646.  
  647. {
  648.     WindowPtr    aPort;
  649.  
  650.  
  651.     GetPort (&aPort);
  652.     SetPort ((WindowPtr) dsp);
  653.         MoveTo (x1, y1);
  654.         LineTo (x2, y2);
  655.     SetPort (aPort);
  656. }
  657.  
  658.  
  659.     void
  660. XDrawPoint (Display *dsp, Drawable win, GC gc, int x, int y)
  661.  
  662. {
  663.     WindowPtr    aPort;
  664.  
  665.  
  666.     GetPort (&aPort);
  667.     SetPort ((WindowPtr) dsp);
  668.         MoveTo (x, y);
  669.         LineTo (x, y);
  670.     SetPort (aPort);
  671. }
  672.  
  673.  
  674. Bool StopKey ()
  675. {    
  676.     EventRecord    ev;
  677.  
  678.  
  679.     if (EventAvail (keyDownMask | autoKeyMask, &ev))
  680.     {
  681.         if ((ev.modifiers & cmdKey) &&
  682.             ((char) (ev.message & charCodeMask) == '.'))
  683.         {
  684.             SysBeep (1);
  685.             (void) GetNextEvent (keyDownMask |autoKeyMask, &ev);
  686.             return True;
  687.         }
  688.     }
  689.  
  690.     return False;
  691. }
  692.  
  693.  
  694.     static Bool
  695. isPressed (unsigned short k) // k =  any keyboard scan code, 0-127
  696.  
  697. {
  698.     unsigned char km[16];
  699.  
  700.  
  701.     GetKeys ((long *) km);
  702.     return ((km[k >> 3] >> (k & 0x07)) & 0x01);
  703. }
  704.  
  705.  
  706. Bool cmdKeyIsDown (void)
  707. {
  708.     return isPressed (55);
  709. }
  710.  
  711.  
  712. Bool shiftKeyIsDown (void)
  713. {
  714.     return isPressed (56);
  715. }
  716.  
  717.  
  718. Bool capsLockIsDown (void)
  719. {
  720.     return isPressed (57);
  721. }
  722.  
  723.  
  724. Bool optionKeyIsDown (void)
  725. {
  726.     return isPressed (58);
  727. }
  728.  
  729.  
  730. Bool MouseButton (void)
  731. {
  732.     EventRecord    ev;
  733.     Bool        retVal = EventAvail (mouseUp, &ev);
  734.  
  735.  
  736.     if (retVal)
  737.         FlushEvents (everyEvent, 0);
  738.  
  739.     return retVal;
  740. }
  741.  
  742.  
  743.     Bool
  744. Keypress (int *pKeypressed)
  745.  
  746. {
  747.     EventRecord    ev;
  748.     Bool        status = EventAvail (keyDownMask | keyUpMask | autoKeyMask, &ev);
  749.  
  750.  
  751.     if (status)
  752.     {
  753.         exitcheck ();
  754.  
  755.         *pKeypressed = ev.message & charCodeMask;
  756.         FlushEvents (everyEvent, 0);
  757.     }
  758.  
  759.     return status;
  760. }
  761.  
  762.  
  763. char *StdGetFile(
  764.     char*  prompt,
  765.     OSType fileTypes[],
  766.     int       nFileTypes)
  767. {
  768.     Point            wher;             /*where to display dialog*/
  769.     SFReply               reply;           /*reply record*/
  770.     short              len;
  771.     static char        filename[80] = "\0";
  772.     
  773.     wher.h = 80;
  774.     wher.v = 90;
  775.     if (optionKeyIsDown()) nFileTypes=0;
  776.  
  777.     SFGetFile(wher, prompt, nil, nFileTypes, fileTypes, nil, &reply);
  778.  
  779.     if (reply.good) {
  780.           len = SetVol(nil, reply.vRefNum);
  781.         len = reply.fName[0];
  782.         strncpy(filename, (char *)(&reply.fName[1]), len);
  783.         filename[len]= '\0';
  784.         return filename;
  785.         }
  786.     else
  787.         return NULL;
  788. }
  789.  
  790.  
  791. // usleep(10000);    == sleep for 1/100th of a second
  792.  
  793. int usleep( unsigned long usec)
  794. {
  795.     long finalTicks = 0;
  796.  
  797.  
  798.     usec >>= 14;    // usec /= 16667;
  799.  
  800.     Delay ((usec < 1) ? 1 : usec, &finalTicks);
  801.  
  802.     return 0;
  803. }
  804.  
  805. long hundredthsofseconds()
  806. {
  807.   //return  (long) (100 * TickCount() / 60);  /* == 100ths of seconds since startup */
  808.   return  TickCount() << 1;  /* == 60ths of seconds since startup */
  809. }
  810.  
  811.  
  812. static short alog2 (register short x);
  813.  
  814.  
  815.     void
  816. XExit (int status)
  817.  
  818. {
  819.     short        nColorEntries = (*theColorTab)->ctSize;
  820.     short        nColorDepth      = alog2 (nColorEntries);
  821.     CTabHandle    hColorTab      = (CTabHandle) GetResource ('clut', 4000 + nColorDepth);
  822.  
  823.  
  824.     FlushEvents (everyEvent, 0);
  825.  
  826. #if 1
  827.     /*...Restore the default CLUT...*/
  828.  
  829.     if (hColorTab /* != (CTabHandle) NULL */)
  830.     {
  831.         (*hColorTab)->ctSeed  = GetCTSeed ();
  832.         (*winpixmap)->pmTable = hColorTab;
  833.         CopyBitsPrep ((Display *) cw, TRUE);
  834.         ShowWindow ((WindowPtr) cw);
  835.         ActivatePalette ((WindowPtr) cw);
  836.     }
  837. #endif
  838.  
  839. #if 1
  840.     XCloseDisplay ((Display *) cw);
  841. #endif
  842.  
  843.     if (status /* != 0 */)
  844.         console_options.pause_atexit = 1;
  845.  
  846.     InitCursor ();
  847.  
  848.     exit (status);
  849. }
  850.  
  851.  
  852. static    RGBColor    colorW     = {0xFFFF, 0xFFFF, 0xFFFF};
  853. static     RGBColor    colorB     = {0x0000, 0x0000, 0x0000};
  854.  
  855.  
  856.     static void
  857. SetPortPenColors (Display *dsp, RGBColor *pRGBForeColor, RGBColor *pRGBBackColor)
  858.  
  859. {
  860.     if (dsp /* != (Display *) NULL */)
  861.     {
  862.         GrafPtr        oldPort;
  863.         CGrafPtr    pPort = (CGrafPtr) dsp;
  864.  
  865.  
  866.         GetPort (&oldPort);
  867.         SetPort ((WindowPtr) dsp);
  868.  
  869.         pPort->txMode = srcCopy;
  870.  
  871.         if (pRGBForeColor /* != (RGBColor *) NULL */)
  872. #if 1
  873.             pPort->rgbFgColor = *pRGBForeColor;
  874. #else
  875.             RGBForeColor (pRGBForeColor);
  876. #endif
  877.  
  878.         if (pRGBBackColor /* != (RGBColor *) NULL */)
  879. #if 1
  880.             pPort->rgbBkColor = *pRGBBackColor;
  881. #else
  882.             RGBBackColor (pRGBBackColor);
  883. #endif
  884.  
  885.         SetPort (oldPort);
  886.     }
  887. }
  888.  
  889.  
  890.     static void
  891. CopyBitsPrep (Display *dsp, char fSave)
  892.  
  893. {
  894.     static RGBColor    rgbFore;
  895.     static RGBColor    rgbBack;
  896.  
  897.  
  898.     if (fSave)
  899.     {
  900.         CGrafPtr    pPort = (CGrafPtr) dsp;
  901.  
  902.  
  903.         rgbFore = pPort->rgbFgColor;
  904.         rgbBack = pPort->rgbBkColor;
  905.  
  906.         SetPortPenColors (dsp, &colorB, &colorW);
  907.     }
  908.     else
  909.     {
  910.         SetPortPenColors (dsp, &rgbBack, &rgbFore);
  911.     }
  912. }
  913.  
  914.  
  915.     void
  916. XSetBlackAndWhite (Display *dsp, Drawable win)
  917.  
  918. {
  919.     if (theColorTab /* != (CTabHandle) NULL */ &&
  920.         (**theColorTab).ctSize /* > 0 */)
  921.     {
  922.         (**theColorTab).ctTable[0].rgb = colorW;
  923.         (**theColorTab).ctTable[1].rgb = colorB;
  924.     }
  925.  
  926.     SetPortPenColors (dsp, &colorB, &colorW);
  927. }
  928.  
  929.  
  930.     static short
  931. alog2 (register short x)
  932.  
  933. {
  934.     register short i = 0;
  935.  
  936.     while (x)
  937.     {
  938.         i++;
  939.         x >>= 1;
  940.     }
  941.  
  942.     return i;
  943. }
  944.